package com.tybbank.Utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Bean.BeiZhuBean;
import com.tybbank.Bean.imagebenan;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import okhttp3.Call;

public class ActivityUtil {
    static ArrayList<Activity> mActivities = new ArrayList<Activity>();
    private static int USIVE_EXCLUSIVE;

    //退出全部的Actvity
    public static void exitAll() {
        for (Activity activity : mActivities) {
            activity.finish();
        }
        mActivities.clear();
    }

    //增加Activity
    public static void add(Activity activity) {
        if (!mActivities.contains(activity)) {
            mActivities.add(activity);
        }
    }

    public static SpannableString SpannableString(String value) {

        SpannableString spannableString = new SpannableString(value);
        if (value.contains(".")) {
            spannableString.setSpan(new RelativeSizeSpan(0.5f), value.indexOf("."), value.length(), USIVE_EXCLUSIVE);
        }
// textView.setText(spannableString);
        return spannableString;
    }

    static String fanhuistring = "";
    static String imageurl = "";
//  图片上传的 公共接口

    public static String imagepath(final Activity activity, final String url, final File file, final ImageView ivAddWithHand) {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(activity, "正在加载...");
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.upload)
                .addFile("image", url, file)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .readTimeOut(30000)
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(activity,  (R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG公共图片上传"+ response.toString());
                        postReponse(response);
                        dialog.dismiss();
                    }

                    public void postReponse(String json) {
                        try{
                            imagebenan enterBean = new Gson().fromJson(json, new TypeToken<imagebenan>() {
                            }.getType());
                            if (enterBean.getCode()==1011){
                                imageurl = enterBean.getData();
                                MyApplication.imageurltwo = enterBean.getData();
                                Glide.with(activity).load(url).into(ivAddWithHand);
                            }else {
                                Toast.makeText(activity, enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }catch (Exception e){
                        }
                    }
                });
        return imageurl;
    }



    // 交易中的配置信息  所有的备注信息
    public static String beizhu(final Activity activity, final int biaoji, final TextView tvBeizhu) {
        fanhuistring = "";
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        OkHttpUtils
                .post()
                .url(Constant.tips_list)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        Toast.makeText(activity, R.string.error, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("TAG备注信息", response.toString());
                        postReponse(response);
                    }

                    public void postReponse(String json) {

                        BeiZhuBean enterBean = new Gson().fromJson(json, new TypeToken<BeiZhuBean>() {
                        }.getType());
                        if (enterBean.getCode() == 1011) {
                            MyApplication.lllllllllll = enterBean.getStatic_bonus();
                            if (biaoji == 1) {
                                if (enterBean.getTips_interturn().size()>=1){
                                    List tips_propose = enterBean.getTips_interturn();
                                    for (int i = tips_propose.size() -1; i>= 0; i--) {
                                        fanhuistring = "     "+(i+1)+": "+tips_propose.get(i) + "\n" + fanhuistring;
                                    }
                                    tvBeizhu.setText("提速说明:\n" + fanhuistring);
                                }
                            } else if (biaoji == 2) {
                                if (enterBean.getTips_regular().size()>=1){
                                    List tips_propose = enterBean.getTips_regular();
                                    for (int i = tips_propose.size() -1; i>= 0; i--) {
                                        fanhuistring = "     "+(i+1)+": "+tips_propose.get(i) + "\n" + fanhuistring;
                                    }
                                    tvBeizhu.setText("定期说明:\n" + fanhuistring);
                                }

                            }  else  if (biaoji == 6) {
                                if (enterBean.getTips_recharge().size()>=1){
                                    List tips_propose = enterBean.getTips_recharge();
                                    for (int i = tips_propose.size() -1; i>= 0; i--) {
                                        fanhuistring = "     "+(i+1)+": "+tips_propose.get(i) + "\n" + fanhuistring;
                                    }
                                    tvBeizhu.setText("充值说明:\n" + fanhuistring);
                                }
                            } else  if (biaoji == 7) {
                                if (enterBean.getTips_propose().size()>=1){
                                    List tips_propose = enterBean.getTips_propose();
                                    for (int i = tips_propose.size() -1; i>= 0; i--) {
                                        fanhuistring = "     "+(i+1)+": "+tips_propose.get(i) + "\n" + fanhuistring;
                                    }
                                    tvBeizhu.setText("提现说明:\n" + fanhuistring);
                                }
                            }
                        } else if (enterBean.getCode() == 1004) {
                            Intent intent = new Intent(activity, LoginActivity.class);
                            activity.startActivity(intent);

                        }
                    }
                });
        return fanhuistring;
    }
}
